<?php

namespace App\Controller;

use App\Logic\ChatLogic;
use Hyperf\Context\ApplicationContext;
use Hyperf\View\RenderInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

class ChatController extends AbstractController
{
    public function chatStream(ChatLogic $logic)
    {
        $input = $this->request->all();

        $logic->chatStream($input);
    }

    /**
     * Describe:
     * @param RenderInterface $render
     * @return \Psr\Http\Message\ResponseInterface
     * Created by crh at 2023/07/02 12:14
     */
    public function html(RenderInterface $render): \Psr\Http\Message\ResponseInterface
    {
        return $render->render('chat', ['name' => '666']);
    }


    /**
     * Describe: 分块传输编码 Chunk
     *        文档：https://hyperf.wiki/3.0/#/zh-cn/response?id=%e5%88%86%e5%9d%97%e4%bc%a0%e8%be%93%e7%bc%96%e7%a0%81-chunk
     * @param ResponseInterface $response
     * @return string
     * Created by crh at 2023/07/03 10:38
     */
    public function chunk(ResponseInterface $response)
    {
        for ($i = 0; $i < 500; $i++) {
            usleep(2000);
            $response->write(json_encode(['data' => ['id' => $i]]) . PHP_EOL);
        }

        return 'Hello Hyperf'; // 使用了chunk, 这边代码不会执行
    }

    /**
     * Describe: 分块传输编码 Chunk
     *        文档：https://hyperf.wiki/3.0/#/zh-cn/response?id=%e5%88%86%e5%9d%97%e4%bc%a0%e8%be%93%e7%bc%96%e7%a0%81-chunk
     * @param ResponseInterface $response
     * @return string
     * Created by crh at 2023/07/03 10:38
     */
    public function chunkHtml(RenderInterface $render)
    {
        return $render->render('chunk', ['name' => '分块传输编码 Chunk']);
    }
}